package electricity.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import electricity.domain.DeviceInfo;
import electricity.domain.DeviceParameter;
import electricity.dto.DeviceInfoDto;
import electricity.dto.DeviceQueryDTO;
import electricity.enumeration.DeviceStatusEnum;
import electricity.mapper.DeviceInfoMapper;
import electricity.mapper.DeviceParameterMapper;
import electricity.mapper.DeviceTypeMapper;
import electricity.mapper.HighRiskDeviceMapper;
import electricity.service.DeviceService;
import electricity.vo.*;
import kotlin.NoWhenBranchMatchedException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private DeviceTypeMapper deviceTypeMapper; // 设备类型Mapper
    @Autowired
    private DeviceVO deviceVO;
    @Autowired
    private DeviceParameterMapper deviceParameterMapper;
    @Autowired
    private HighRiskDeviceMapper highRiskDeviceMapper;

    /**
     * 分页查询
     *
     * @param deviceQueryDTO
     * @return
     */
    public PageResult pageQuery(DeviceQueryDTO deviceQueryDTO) {
        PageHelper.startPage(deviceQueryDTO.getPage(), deviceQueryDTO.getPageSize());
        Page<DeviceVO> page = deviceInfoMapper.pageQuery(deviceQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }


    /**
     * 修改
     * @param deviceInfoDto
     * @return
     */
    public DeviceVO update(DeviceInfoDto deviceInfoDto) {
         log.info("更新设备信息:{}", deviceInfoDto);

        // 更新设备信息
      //  deviceInfoDto.setCreateTime(new Date());
        deviceInfoMapper.updateByPrimaryKey(deviceInfoDto);

        // 查询更新后的设备信息
        DeviceInfo deviceInfo = deviceInfoMapper.selectByPrimaryKey(Long.valueOf(deviceInfoDto.getId()));

       // DeviceVO deviceVO = new DeviceVO();
        BeanUtils.copyProperties(deviceInfo, deviceVO);
       // deviceVO.setCreateTime(new Date());

        return deviceVO;
    }


    /**
     *新增
     * @param deviceInfoDto
     * @return
     */
    public DeviceVO insert(DeviceInfoDto deviceInfoDto) {
        log.info("新增设备信息:{}", deviceInfoDto);
        DeviceInfo deviceInfo = new DeviceInfo();
        BeanUtils.copyProperties(deviceInfoDto, deviceInfo);
        deviceInfo.setCreateTime(new Date());
        deviceInfoMapper.insert(deviceInfo);
        BeanUtils.copyProperties(deviceInfo, deviceVO);
        return deviceVO;
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Integer id) {
        log.info("删除设备:{}", id);
        deviceInfoMapper.deleteByPrimaryKey(Long.valueOf(id));

    }

    public void batchRemove(List<Integer> ids) {
        log.info("批量删除设备:{}", ids);
        for (Integer id : ids) {
            deviceInfoMapper.deleteByPrimaryKey(Long.valueOf(id));
            //TODO 删除关联的记录
        }
    }

    /**
     * 修改设备状态
     * @param deviceInfoDto
     * @return
     */
    public DeviceVO updatestatus(DeviceInfoDto deviceInfoDto) {
        log.info("修改设备状态:{}", deviceInfoDto);
        DeviceInfo deviceInfo = deviceInfoMapper.selectByPrimaryKey(Long.valueOf(deviceInfoDto.getId()));
        deviceInfo.setStatus(deviceInfoDto.getStatus());
        deviceInfoMapper.updateByPrimaryKeySelective(deviceInfo);

        // 查询更新后的设备信息
      //  DeviceInfo deviceInfo1 = deviceInfoMapper.selectByPrimaryKey(Long.valueOf(deviceInfoDto.getId()));
        BeanUtils.copyProperties(deviceInfo, deviceVO);

        return deviceVO;
    }

    @Override
    public PageResult paramspageQuery(@RequestBody DeviceQueryDTO deviceQueryDTO) {
        PageHelper.startPage(deviceQueryDTO.getPage(), deviceQueryDTO.getPageSize());
        Page<DeviceParamsVo> page = deviceInfoMapper.paramspageQuery(deviceQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<String> getParamNamesByDeviceId(Integer deviceId) {
       List<String> paramNames = deviceParameterMapper.getParamNamesByDeviceId(deviceId);
       return paramNames;
    }

    @Override
    public HealthyVo gethealthStats() {
       HealthyVo healthyVo =deviceInfoMapper.gethealthStats();

       return healthyVo;

    }

    @Override
    public List<HighRiskVo> getHighRiskDevices(Integer deviceId) {

        List<HighRiskVo> highRiskDevices = highRiskDeviceMapper.getHighRiskDevices(deviceId);
        return highRiskDevices;
    }
}
